import { parseTime } from '@/utils/index.js'
/* 用户 */
const userInfo = () => {
  return {
    status: 1,
    data: {
      id: 1,
      mobile: 18888888888,
      nickname: '张三',
      portrait: 'http://img.61ef.cn/news/201409/28/2014092805595807.jpg'
    },
    msg: '提示'
  }
}
const addressList = [
  {
    id: 1,
    name: '哈哈哈',
    mobile: '18666666666',
    addressName: '山东得益乳业股份有限公司',
    address: '山东省淄博市张店区裕民路135号',
    area: 'B区',
    latitude: 36.865497,
    longitude: 118.062759,
    isDefault: 0
  },{
    id: 2,
    name: '呵呵呵',
    mobile: '18667766666',
    addressName: '正泰大厦',
    address: '浙江省杭州市滨江区月明路560号',
    area: '1号楼5楼',
    latitude: 30.206087903,
    longitude: 120.221772589,
    isDefault: 1,
  }
]
const indexInfo = () => { 
  return {
  /* 首页轮播图 */
  bannerList: [
    {
      src: '/static/temp/banner3.jpg',
      background: 'rgb(203, 87, 60)',
    },
    {
      src: '/static/temp/banner2.jpg',
      background: 'rgb(205, 215, 218)',
    },
    {
      src: '/static/temp/banner4.jpg',
      background: 'rgb(183, 73, 69)',
    }
  ],
  // 首页分类列表
  navInfoList: [
    {
      id: 1,
      image: '/static/temp/c3.png',
      title: '微商城'
    },
    {
      id: 2,
      image: '/static/temp/c8.png',
      title: '低温鲜奶'
    },
    {
      id: 3,
      image: '/static/temp/c6.png',
      title: '低温酸奶'
    },
    {
      id: 4,
      image: '/static/temp/c7.png',
      title: '新品'
    },
  ],
  /* 首页商品列表 */
  hotGoodList: [
    {
      goodId: 1,
      goodImg: '//img13.360buyimg.com/n1/jfs/t1/49643/25/1122/52105/5ceded02E763d43f4/1c3ecd564062c83d.jpg',
      goodName: '臻优高品质鲜牛奶200ml',
      goodPrice: 13.00,
    },
    {
      goodId: 2,
      goodImg: '//img12.360buyimg.com/n1/jfs/t1/48332/27/14087/111317/5dad5a3cEf5be9ee3/5f236b21f971929a.jpg',
      goodName: '鲜境高品质鲜牛奶450ml',
      goodPrice: 15.00,
    },
    {
      goodId: 3,
      goodImg: '//img14.360buyimg.com/n1/jfs/t1/1064/24/9179/431293/5bac493eEd27c9dbf/0c66c97514bd0f4a.png',
      goodImg1: '//img14.360buyimg.com/n1/jfs/t1/116027/4/3127/225795/5ea63151E6301c615/3c553ba9ed877d1c.jpg',
      goodName: '优麦风味酸奶发酵乳220g',
      goodPrice: 7.00,
    },
    {
      goodId: 4,
      goodImg: '//img10.360buyimg.com/n1/jfs/t1/101878/37/423/106622/5dad5965Efb9e3c30/18d79f2c913af0c3.jpg',
      goodName: '鲜境高品质鲜牛奶200ml',
      goodPrice: 8.00,
    },
  ]
}
}
// 商品列表
const goodList = [
  {
    goodId: 1,
    goodImgs: [
      '//img13.360buyimg.com/n1/jfs/t1/49643/25/1122/52105/5ceded02E763d43f4/1c3ecd564062c83d.jpg'
    ],
    goodName: '臻优高品质鲜牛奶200ml',
    goodPrice: 13.00,
    activityInfo: [
      {
        id: 1,
        activityName: '',
        activityType: 1, // 1 满送 2 满减 3 满折
      }
    ],
    detailGoodImgs: [
      '//img13.360buyimg.com/n1/jfs/t1/49643/25/1122/52105/5ceded02E763d43f4/1c3ecd564062c83d.jpg',
      '//img13.360buyimg.com/n1/jfs/t1/118339/28/3131/176788/5ea63150Ecda0bbdf/a404ae9adc9634e2.jpg'
    ],
  },
  {
    goodId: 2,
    goodImgs: [
      '//img12.360buyimg.com/n1/jfs/t1/48332/27/14087/111317/5dad5a3cEf5be9ee3/5f236b21f971929a.jpg'
    ],
    goodName: '鲜境高品质鲜牛奶450ml',
    goodPrice: 15.00,
    activityInfo: [
      {
        id: 1,
        activityName: '',
        activityType: 2, // 1 满送 2 满减 3 满折
      }
    ],
    detailGoodImgs: [
      '//img13.360buyimg.com/n1/jfs/t1/49643/25/1122/52105/5ceded02E763d43f4/1c3ecd564062c83d.jpg',
      '//img13.360buyimg.com/n1/jfs/t1/118339/28/3131/176788/5ea63150Ecda0bbdf/a404ae9adc9634e2.jpg'
    ],
  },
  {
    goodId: 3,
    goodImgs: [
      '//img14.360buyimg.com/n1/jfs/t1/1064/24/9179/431293/5bac493eEd27c9dbf/0c66c97514bd0f4a.png',
      '//img14.360buyimg.com/n1/jfs/t1/116027/4/3127/225795/5ea63151E6301c615/3c553ba9ed877d1c.jpg'
    ],
    goodName: '优麦风味酸奶发酵乳220g',
    goodPrice: 7.00,
    activityInfo: [
      {
        id: 1,
        activityName: '',
        activityType: 3, // 1 满送 2 满减 3 满折
      }
    ],
    detailGoodImgs: [
      '//img13.360buyimg.com/n1/jfs/t1/49643/25/1122/52105/5ceded02E763d43f4/1c3ecd564062c83d.jpg',
      '//img13.360buyimg.com/n1/jfs/t1/118339/28/3131/176788/5ea63150Ecda0bbdf/a404ae9adc9634e2.jpg'
    ],
  },
  {
    goodId: 4,
    goodImgs: [
      '//img10.360buyimg.com/n1/jfs/t1/101878/37/423/106622/5dad5965Efb9e3c30/18d79f2c913af0c3.jpg',
    ],
    goodName: '鲜境高品质鲜牛奶200ml',
    goodPrice: 8.00,
    activityInfo: [
      {
        id: 1,
        activityName: '',
        activityType: 1, // 1 满送 2 满减 3 满折
      }
    ],
    detailGoodImgs: [
      '//img13.360buyimg.com/n1/jfs/t1/49643/25/1122/52105/5ceded02E763d43f4/1c3ecd564062c83d.jpg',
      '//img13.360buyimg.com/n1/jfs/t1/118339/28/3131/176788/5ea63150Ecda0bbdf/a404ae9adc9634e2.jpg'
    ],
  },
  
]
// 订单列表
const orderList = [
{
  deliveryAddress:  '杭州市滨江区',
  orderDetail: {
    alreadyDeliveryTimes:1,
    avgNumPreDay:1,
    couponAmount:0,
    deliveryAddress:'杭州市滨江区',
    deliveryEnd:'2020-07-30',
    deliveryModel:1,
    deliveryStart:'2020-07-08',
    deliveryTimes:23,
    goodId:'3',
    goodImg:'//img14.360buyimg.com/n1/jfs/t1/1064/24/9179/431293/5bac493eEd27c9dbf/0c66c97514bd0f4a.png',
    goodName:'优麦风味酸奶发酵乳220g',
    goodPrice:7,
    payAmount:'161.00',
    payType: 1,
    postAmount: 0,
    receiverMobile: '13853242563',
    receiverName: '张三',
    totalAmount: '161.00',
    totalNum: 23,
  },
  orderId: 1,
  orderNo: '1593259472166',
  orderStatus: 2,
  orderTime: '2020-06-27 20:04:32',
  receiverMobile: '13853242563',
  receiverName: '张三',

},

]
// 配送列表
const orderDeliveryPlanList = [
  {
    orderNo: '1593259472166',
    openId: '',
    deliveryPlanList: [
      {
        deliveryDate: '2020-07-08',
        deliveryStatus: 1,
        isStopped: 0
      },
      {
        deliveryDate: '2020-07-09',
        deliveryStatus: 1,
        isStopped: 0
      },
      {
        deliveryDate: '2020-07-10',
        deliveryStatus: 1,
        isStopped: 0
      },
      {
        deliveryDate: '2020-07-11',
        deliveryStatus: 1,
        isStopped: 1
      },
      {
        deliveryDate: '2020-07-12',
        deliveryStatus: 1,
        isStopped: 1
      },
      {
        deliveryDate: '2020-07-13',
        deliveryStatus: 1,
        isStopped: 0
      },
      {
        deliveryDate: '2020-07-14',
        deliveryStatus: 1,
        isStopped: 0
      },
    ]
  }
]

// 商品详情
const getGoodDetail = ({ goodId }) => {
  return goodList[goodId - 1]
}
// 获取商品列表
const getGoods = ({ pageNo, pageSize }) => {
  return goodList
}

// 下单
const order = ({
  deliveryAddress,
  deliveryProvince,
  deliveryCity,
  deliveryTown,
  receiverMobile,
  receiverName,
  deliveryModel,
  deliveryStart,
  deliveryEnd,
  avgNumPreDay,
  totalNum,
  goodId,
  totalAmount,
  couponAmount,
  postAmount,
  payAmount
}) => {
  const orderNo = +new Date()
  if(orderList.findIndex(item => String(item.orderNo) === String(orderNo)) > -1) {
    return
  }
  orderList.push({
    orderNo: orderNo + '',
    orderId: orderList.length + 1,
    orderStatus: 1,
    orderTime: parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}'),
    orderDetail: {
      goodId,
      goodName: goodList[goodId - 1].goodName,
      goodPrice: goodList[goodId - 1].goodPrice,
      goodImg: goodList[goodId - 1].goodImgs[0],
      deliveryModel,
      avgNumPreDay,
      totalNum,
      deliveryStart,
      deliveryEnd,
      totalAmount,
      couponAmount,
      postAmount,
      payAmount,
      deliveryAddress,
      receiverMobile,
      receiverName,
      deliveryTimes: totalNum / avgNumPreDay,
      alreadyDeliveryTimes: 1
    },
    receiverMobile,
    receiverName,
    deliveryAddress
  })
  getOrderDeliveryPlan({ orderNo })
  
  return orderNo
}
// 支付成功
const payOrder = ({ orderNo }) => {
  const index = orderList.findIndex(item => item.orderNo === orderNo + '')
  orderList[index].orderStatus = orderList[index].extraOrder ? 4 : 2
  orderList[index].orderDetail.payType = 1
  return {
    code: 0,
    data: '支付成功',
    msg: '支付成功'
  }
}

// 根据orderNo订单详情
const getOrderDetail = ({ orderNo }) => {
  // console.log(orderNo, orderList)
  const orderDetail = orderList.find(item => item.orderNo === orderNo)
  return orderDetail
}
// 根据订单获取配送计划
const getOrderDeliveryPlan = ({ orderNo }) => {
  const orderItem = orderList.find(item => String(item.orderNo) === String(orderNo))
  const orderDetail = orderItem.orderDetail
  const startDate = orderDetail.deliveryStart
  // const endDate = orderDetail.deliveryEnd
  const goodTotalNum = orderDetail.totalNum / orderDetail.avgNumPreDay
  const goodNum = orderDetail.avgNumPreDay
  const deliveryPlanList = []
  for(let index = 0; index <= goodTotalNum; index++) {
    if(index === goodTotalNum) {
      break
    }
    const obj = {}
    obj.deliveryDate = parseTime(new Date(startDate).getTime() + 3600000 * 24 * index, '{y}-{m}-{d}')
    obj.deliveryStatus = index === 0 ? 2 : 1
    obj.deliveryItemList = [
      {
        deliveryTime: startDate,
        goodName: orderDetail.goodName,
        goodImg: orderDetail.goodImg,
        goodNum,
        goodTotalNum,
        deliveryStatus: 1
      }
    ]
    deliveryPlanList.push(obj)
  }
  const userInfo = uni.getStorageSync('userInfo') || ''
  // console.log(orderDeliveryPlanList.findIndex(item => item.orderNo === String(orderNo)))
  if(orderDeliveryPlanList.findIndex(item => String(item.orderNo) === String(orderNo)) === -1) {
    orderDeliveryPlanList.push({
      orderNo,
      openId: userInfo.openId,
      deliveryPlanList
    })
  }
  
  return deliveryPlanList
}
// 获取订奶配送的列表
const getStopOrderPlan = ({ openId, orderNo }) => {
  const orderPlan = orderDeliveryPlanList.find(item => String(item.orderNo) === String(orderNo))
  orderPlan.deliveryPlanList = orderPlan.deliveryPlanList.filter(item => item.deliveryStatus === 1)
  return orderPlan
}

// 改奶
const calcChangeOrder = ({ openId, orderNo, goodId, planDeliveryList }) => {
  const goodItem = goodList.find(item => item.goodId === goodId)
  const orderItem = orderList.find(item => item.orderNo === String(orderNo))
  const priceAmount = (goodItem.goodPrice - orderItem.orderDetail.goodPrice) * planDeliveryList.length
  if (priceAmount > 0) {
    const newOrderNo = +new Date()
    orderList.push(
    {
      orderNo: newOrderNo + '',
      orderId: orderList.length + 1,
      orderStatus: 1,
      extraOrder: 1,
      orderTime: parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}'),
      orderDetail: {
        goodId,
        goodName: '改奶差价',
        changeBefore: orderItem.orderDetail.goodName,
        changeAfter: goodItem.goodName,
        changeBeforePrice: orderItem.orderDetail.goodPrice,
        changeAfterPrice: goodItem.goodPrice,
        goodPrice: goodItem.goodPrice - orderItem.orderDetail.goodPrice,
        goodImg: 'https://gd1.alicdn.com/imgextra/i1/4023772182/O1CN012mq3I51RzOiCAxWd0_!!4023772182.jpg',
        deliveryModel: orderItem.deliveryModel,
        avgNumPreDay: orderItem.avgNumPreDay,
        totalNum: planDeliveryList.length,
        deliveryStart: planDeliveryList[0].deliveryDate,
        deliveryEnd: planDeliveryList[planDeliveryList.length - 1].deliveryDate,
        totalAmount: priceAmount,
        couponAmount: 0,
        postAmount: 0,
        payAmount: priceAmount,
        deliveryAddress: orderItem.deliveryAddress,
        receiverMobile: orderItem.receiverMobile,
        receiverName: orderItem.receiverName,
        // deliveryTimes: totalNum / avgNumPreDay,
        // alreadyDeliveryTimes: 1
      },
      receiverMobile: orderItem.receiverMobile,
      receiverName: orderItem.receiverName,
      deliveryAddress: orderItem.deliveryAddress
    })
    return {
      priceAmount: priceAmount.toFixed(2),
      orderNo: newOrderNo + ''
    }
  } else {
    return {
      priceAmount: priceAmount.toFixed(2)
    }
  }
}
// 停奶
const stopOrder = ({ openId, orderNo, stopType, planDeliveryList, centralizedDate }) => {
  const orderItem = orderList.find(item => item.orderNo === String(orderNo))
  const deliveryPlanItem = orderDeliveryPlanList.find(item => String(item.orderNo) === String(orderNo))
  return {
    code: 0,
    data: '操作成功',
    msg: '操作成功'
  }
}
// 获取我的订单
const getOrders = ({ openId, orderStatus, pageNo, pageSize }) => {
  if(!orderStatus) {
    return orderList.slice((pageNo - 1) * pageSize, pageNo * pageSize)
  }
  let myOrderList = orderList.filter(item => item.orderStatus === orderStatus)
  myOrderList = myOrderList.slice((pageNo - 1) * pageSize, pageNo * pageSize)
  // console.log(myOrderList)
  return myOrderList
}
// 获取我的送奶日历
const getOrderCalendar = ({ openId, deliveryTime }) => {
  // console.log(orderDeliveryPlanList)
  const orderDeliveryPlanListFilter = orderDeliveryPlanList.filter(item => item.openId === openId)
  return orderDeliveryPlanListFilter[0].deliveryPlanList
}
// 获取我的最新的送奶计划
const getUserInfoData = ({ openId }) => {
  // let orderDeliveryPlanListFilter = orderDeliveryPlanList.filter(item => item.openId === openId)
  // orderDeliveryPlanListFilter = orderDeliveryPlanListFilter.length === 0 ? [[]] : orderDeliveryPlanListFilter
  // console.log(orderDeliveryPlanListFilter)
  return {
    lastDeliveryTime: '2020-07-01',
    lastDeliveryStatus: 1,
    serviceOrders: 3,
    notPayOrders: 4,
    deliveryInfoList: [
      {
        goodName: '优麦风味酸奶发酵乳220g',
        goodImg: '//img14.360buyimg.com/n1/jfs/t1/1064/24/9179/431293/5bac493eEd27c9dbf/0c66c97514bd0f4a.png'
      }
    ]
  }
}
// 获取用户地址列表
const getAddressList = ({ openId }) => {
  return addressList
}
const addAddress = (args) => {
  if(args['isDefault']) {
    addressList.map(item => {
      item.isDefault = 0
    })
  }
  addressList.unshift({ ...args })
  return {
    code: 0,
    data: '添加成功',
    msg: '添加成功'
  }
}
const editAddress = (args) => {
  if(args['isDefault']) {
    addressList.map(item => {
      item.isDefault = 0
    })
  }
  const data = { ...args }
  const id = data.id
  delete data.id
  const index = addressList.findIndex(item => +item.id === +id)
  addressList[index] = data
  // console.log(addressList)
  return {
    code: 0,
    data: '修改成功',
    msg: '修改成功'
  }
}
// 投送的日历列表
const selectedDateDeliverList = [
  {
    date: '2020-06-24',
    info: '已送',
    deliveryStatus: 2,
    deliveryItemList: [
      {
        goodImg: '/static/errorImage.jpg',
        goodName: '优麦250ml',
        goodNum: 2,
        deliveryStatus: 2
      },
      {
        goodImg: '/static/errorImage.jpg',
        goodName: '优麦500ml',
        goodNum: 1,
        deliveryStatus: 2
      },
    ]
  },
  {
    date: '2020-06-25',
    info: '已送',
    deliveryStatus: 2,
    deliveryItemList: [
      {
        goodImg: '/static/errorImage.jpg',
        goodName: '优麦250ml',
        goodNum: 2,
        deliveryStatus: 2
      },
      {
        goodImg: '/static/errorImage.jpg',
        goodName: '优麦500ml',
        goodNum: 1,
        deliveryStatus: 2
      },
    ]
  },
  {
    date: '2020-06-26',
    info: '已送',
    deliveryStatus: 2,
    deliveryItemList: [
      {
        goodImg: '/static/errorImage.jpg',
        goodName: '优麦250ml',
        goodNum: 2,
        deliveryStatus: 2
      },
    ]
  },
  {
    date: '2020-06-27',
    info: '待配送',
    deliveryStatus: 1,
    deliveryItemList: [
      {
        goodImg: '/static/errorImage.jpg',
        goodName: '优麦250ml',
        goodNum: 2,
        deliveryStatus: 1
      },
    ]
  },
  {
    date: '2020-06-28',
    info: '待配送',
    deliveryStatus: 1,
    deliveryItemList: [
      {
        goodImg: '/static/errorImage.jpg',
        goodName: '优麦250ml',
        goodNum: 2,
        deliveryStatus: 1
      },
    ]
  },
  {
    date: '2020-06-29',
    info: '待配送',
    deliveryStatus: 1,
    deliveryItemList: [
      {
        goodImg: '/static/errorImage.jpg',
        goodName: '优麦250ml',
        goodNum: 2,
        deliveryStatus: 1
      },
    ]
  },
]

export default {
  indexInfo,
  getGoodDetail,
  order,
  payOrder,
  getOrderDetail,
  getOrderDeliveryPlan,
  getStopOrderPlan,
  getGoods,
  calcChangeOrder,
  stopOrder,
  userInfo,
  getOrders,
  getOrderCalendar,
  getUserInfoData,
  getAddressList,
  addAddress,
  editAddress,
  selectedDateDeliverList
}
